package tgc.edu.examxy.service;

import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import tgc.edu.examxy.custom.CommonService;
import tgc.edu.examxy.custom.TimeUtils;
import tgc.edu.examxy.entity.TbClass;
import tgc.edu.examxy.repository.TbClassRepository;

@Service
public class TbClassService extends CommonService<TbClass, UUID> {

	@Autowired
	private TbClassRepository tbClassDAO;

	public List<TbClass> findByTbClassName(String value) {
		
		return tbClassDAO.findByClassStatusStateAndIsUsedAndCtypeType("报名",true,value);
	}

	public String findByTime(UUID uuid) {
		Optional<TbClass> tbclass = tbClassDAO.findById(uuid);
		TbClass tbClass2 = tbclass.get();
		String time = tbClass2.getStartTime() + "——" +tbClass2.getCloseTime();
		return time;
	}
	/***
	 * 判断是否可以报名
	 * @param tbClass
	 * @return
	 */
	public boolean ifTime(TbClass tbClass) {
		Date nowTime = TimeUtils.getNowTime();
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String regBeginTime = tbClass.getRegBeginTime();//报名开始时间
		String regEndTime = tbClass.getRegEndTime();//报名结束时间
		try {
			Date BeginTime = sdf.parse(regBeginTime);
			Date EndTime = sdf.parse(regEndTime);
			int compareTo = nowTime.compareTo(BeginTime);//0 1
			int compareTo2 = EndTime.compareTo(nowTime); //0 1
			if(compareTo>=0&&compareTo2>=0) {
				return true;
			}else {
				return false;
			}
		} catch (ParseException e) {
			System.out.println("时间处理出错");
			e.printStackTrace();
		}
		
		return true;
	}
	/****
	 * 获取这个班级学员的证书编号
	 * @param maxStudent 
	 * @param tbclassEntity
	 */
	public String serical(TbClass tbclass, String maxStudent) {
		String closeTime = "";
		//处理结业时间
		if(tbclass.getCloseTime()!=null) {
			closeTime  = tbclass.getCloseTime().replaceAll("-", "");
		}
		closeTime = closeTime + maxStudent;
		return closeTime;
	}

	public List<TbClass> findByTbClassId(UUID value) {
		return tbClassDAO.findByClassStatusStateAndIsUsedAndCtypeId("报名",true,value);

	}
	
}
